package com.tnt.dashboard;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tnt.dashboard.domain.MenuInfo;

/**
 * BaseAdapter를 상속받아 구현한 HomeMenuGridButtonsAdapter
 * 
 * @author ParkHG
 * @since 2012.09.08
 */
public class HomeMenuGridButtonsAdapter extends BaseAdapter
{
	private ArrayList<MenuInfo> mMenuList;
	private Context mContext;

	/**
	 * Adpater 생성자
	 * 
	 * @param context
	 *            컨텍스트
	 * @param menuList
	 *            메뉴정보가 들어있는 리스트
	 */
	public HomeMenuGridButtonsAdapter(Context context, ArrayList<MenuInfo> menuList)
	{
	    this.mContext = context;
	    this.mMenuList = menuList;
	}

    @Override
    public int getCount()
    {
        return mMenuList.size();
    }
 
    @Override
    public Object getItem(int position)
    {
        return mMenuList.get(position);
    }
 
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        MenuInfo menu = mMenuList.get(position);
        ViewHolder holder = null;

        if (convertView == null) {
            LayoutInflater li = ((Activity) mContext).getLayoutInflater();
            convertView = li.inflate (R.layout.activity_home_button, null);
            
            holder = new ViewHolder();
	        holder.ivIcon = (ImageView)convertView.findViewById(R.id.iv_icon);
            holder.tvMenuName = (TextView)convertView.findViewById(R.id.tv_menuname);
            holder.tvMenuEventCount = (TextView)convertView.findViewById(R.id.menuEventCount);
            
            holder.menuId = menu.getMenuId();
            convertView.setTag(holder);
        } else {
        	holder = (ViewHolder)convertView.getTag();
        }

        // Find the image part and update the image source.
        
        // Find the label part and update the text.
	    holder.ivIcon.setImageBitmap(menu.getMenuIcon());
        holder.tvMenuName.setText(menu.getMenuName());
        if (menu.getMenuEventCount().equals("0")){
        	holder.tvMenuEventCount.setBackgroundColor(Color.TRANSPARENT);
        } else {
        	holder.tvMenuEventCount.setText(menu.getMenuEventCount());        	
        }
        
        /*
        v.mCellNumber = position;
        v.mGrid = (GridView) mParentView;
        v.mEmpty = true;
        v.setBackgroundResource (R.color.drop_target_enabled);
        v.setBackgroundResource (R.color.cell_empty);
        */        
        return convertView;
    }    
    
    static class ViewHolder 
    {
	    ImageView ivIcon;
    	String    menuId;
	    TextView  tvMenuName;
	    TextView  tvMenuEventCount;    	    
    }
}
